<template>
	<view>
		<div class="content native-scroll">
			<div class="avatar flex_xy">
				<div class="">
					<div class="id_card_content">
						<div class="" style="position: absolute;width: 100%;height: 100%;">
							<img class="pic" :src="form.avatar" alt="">
						</div>
						<div class="" style="height: 80px;width: 80px;">
							<!-- <input class="weui-uploader__input" id="piccc" type="file" name="pic_store_business_license"
		                               accept="image/*"> -->
						</div>
					</div>
				</div>
			</div>
			<view class="wrap">
				<u-form :model="form">
					<view class="top">
						<view class="item">
							<view class="left">昵称</view>
							<input type="text" v-model="form.user_nickname" placeholder-class="line"
								placeholder="请填写昵称" />
						</view>
						<view class="item">
							<view class="left">手机号</view>
							<input type="text" v-model="form.mobile" placeholder-class="line" placeholder="请填写手机号" />
						</view>
						<view class="item">
							<view class="left">登录密码</view>
							<input type="text" v-model="form.password" placeholder-class="line"
								placeholder="登录密码不修改无需填写" />
						</view>
						<view class="item">
							<view class="left">姓名</view>
							<input type="text" v-model="form.contact" placeholder-class="line" placeholder="请填写姓名" />
						</view>
						<view class="item">
							<view class="left">品牌</view>
							<!-- @tap="getArea(1)" -->
							<view>
								<input disabled type="text" v-model="brand_one" placeholder-class="line"
									placeholder="品牌" />
							</view>
						</view>
						<view class="item">
							<view class="left">商户号</view>
							<input type="text" v-model="form.memNo" placeholder-class="line"
								placeholder="请填写商户号" />
						</view>
						<view class="item">
							<view class="left">终端号</view>
							<input type="text" v-model="form.termNo" placeholder-class="line"
								placeholder="请填写终端号" />
						</view>
						<!-- <view class="item">
							<view class="left">品牌2</view>
							<view @tap="getArea(2)">
								<input disabled type="text" v-model="brand_two" placeholder-class="line"
									placeholder="品牌2" />
							</view>
						</view>
						<view class="item">
							<view class="left">商户号</view>
							<input type="text" v-model="form.store_number_two" placeholder-class="line"
								placeholder="请填写商户号" />
						</view>
						<view class="item">
							<view class="left">终端号</view>
							<input type="text" v-model="form.store_termno_two" placeholder-class="line"
								placeholder="请填写终端号" />
						</view>
						<view class="item">
							<view class="left">品牌3</view>
							<view @tap="getArea(3)">
								<input disabled type="text" v-model="brand_third" placeholder-class="line"
									placeholder="品牌1" />
							</view>
						</view>
						<view class="item">
							<view class="left">商户号</view>
							<input type="text" v-model="form.store_number_third" placeholder-class="line"
								placeholder="请填写商户号" />
						</view>
						<view class="item">
							<view class="left">终端号</view>
							<input type="text" v-model="form.store_termno_third" placeholder-class="line"
								placeholder="请填写终端号" />
						</view> -->
					</view>
				</u-form>
			</view>

			<!-- 地址选择组件 -->
			<u-select v-model="show1" mode="single-column" :list="areaData" label-name="name" value-name="id"
				@confirm="confirm1"></u-select>

			<!-- 地址选择组件 -->
			<u-select v-model="show2" mode="single-column" :list="areaData" label-name="name" value-name="id"
				@confirm="confirm2"></u-select>

			<!-- 地址选择组件 -->
			<u-select v-model="show3" mode="single-column" :list="areaData" label-name="name" value-name="id"
				@confirm="confirm3"></u-select>

		</div>
		<div class="content-block">
			<button class="button button-big button-fill" style="width: 100%;" @click="update_user()">提交</button>
		</div>
		</div>
	</view>
</template>

<script>
	import {
		mapState
	} from "vuex";
	export default {
		computed: {
			...mapState(["user", "token"]), // vuex获取用户数据
		},
		data() {
			return {
				brand_one: '瑞一通',
				brand_two: '',
				brand_third: '',
				form: {
					store_name_one: '',
					store_name_two: '',
					store_name_third: '',
					store_number_one: '',
					store_number_two: '',
					store_number_third: '',
					user_nickname: '',
					mobile: '',
					password: '',
					store_termno_one: '',
					store_termno_two: '',
					store_termno_third: '',
					memNo:'',
					termNo:'',
					brand_id:'',
					// avatar:'',
				},
				areaData: null,
				show1: false,
				show2: false,
				show3: false,
				brand_list: [{
					id: 1,
					name: '1'
				}, {
					id: 2,
					name: '2'
				}],
				user_info: {}
			}
		},
		onLoad() {
			console.log(uni.getStorageSync("token"));
			if (uni.getStorageSync("token")) {
				this.getUser();
			}
			this.getBrand()
		},
		methods: {
			getBrand() {
				this.$api.get({
					url: 'user/address/getBrand',
					success: res => {
						let area = res.data;
						if (res.code == 1) {
							this.areaData = area;
						}
					}
				});
			},
			getArea(number) {
				if (this.areaData) {
					if (number == 1) {
						this.show1 = true;
					}

					if (number == 2) {
						this.show2 = true;
					}

					if (number == 3) {
						this.show3 = true;
					}


					return;
				}
				// this.$loading();
				// uni.showLoading();
				// this.$api.get({
				// 	url: 'user/address/getBrand',
				// 	success: res => {
				// 		let area = res.data;
				// 		if (res.code == 1) {
				// 			this.areaData = area;
				// 			if (number == 1) {
				// 				this.show1 = true;
				// 			}

				// 			if (number == 2) {
				// 				this.show2 = true;
				// 			}

				// 			if (number == 3) {
				// 				this.show3 = true;
				// 			}
				// 		} else {
				// 			this.$toast(res.msg);
				// 		}
				// 	},
				// 	complete: () => {
				// 		// uni.hideLoading();
				// 	}
				// });
			},
			confirm1(e) {
				// id和name做区分是为了跟PC端的规则同步，不是刻意做复杂的
				if (e[0].label) {
					this.brand_one = e[0].label;
					this.form.store_name_one = e[0].value;
				} else {
					this.brand_one = '';
					this.form.store_name_one = 0;
				}

				// this.brand_one = this.province_name;
			},
			confirm2(e) {
				// id和name做区分是为了跟PC端的规则同步，不是刻意做复杂的
				if (e[0].label) {
					this.brand_two = e[0].label;
					this.form.store_name_two = e[0].value;
				} else {
					this.brand_two = '';
					this.form.store_name_two = 0;
				}
			},
			confirm3(e) {
				// id和name做区分是为了跟PC端的规则同步，不是刻意做复杂的
				if (e[0].label) {
					this.brand_third = e[0].label;
					this.form.store_name_third = e[0].value;
				} else {
					this.brand_third = '';
					this.form.store_name_third = 0;
				}
			},
			getUser() {
				this.$api.get({
					url: "portal/shop/my",
					success: (res) => {
						console.log(res);
						if (res.code == 1) {
							this.form = res.data.users;
							console.log(res.data.pos)
							this.form.brand_one = '瑞一通';
							if(res.data.pos){
								this.form.memNo = res.data.pos.memNo
								this.form.termNo = res.data.pos.termNo
							}
							
							
							// console.log(this.areaData[res.data.users.store_name_one])

							// this.areaData.filter((item, index) => {
							// 	if (item.id == res.data.users.store_name_one) {
							// 		this.brand_one = this.areaData[index]['name'];
							// 	}
							// 	if (item.id == res.data.users.store_name_two) {
							// 		this.brand_two = this.areaData[index]['name'];
							// 	}
							// 	if (item.id == res.data.users.store_name_third) {
							// 		this.brand_third = this.areaData[index]['name'];
							// 	}
							// })
						} else {
							this.$toast("数据获取失败");
						}
					},
				});
			},
			update_user() {

				if (!this.form.user_nickname) {
					this.$toast('请输入昵称!');
				}
				if (!this.form.mobile) {
					this.$toast('请输入手机号!');
				}

				if (this.form.mobile) {
					//正则表达式验证手机号是否正确
					var myreg = /^(13|14|15|16|17|18|19)[0-9]{9}$/;
					if (!myreg.test(this.form.mobile)) {
						this.$toast('请输入正确的手机号!');
						return false;
					}
				}
				
				this.form.store_name_one = 12;

				if (this.form.store_name_one == this.form.store_name_two || this.form.store_name_one == this.form.store_name_third) {
					 this.$toast('请选择不同的终端品牌!');
					return false;
				}

				if (this.form.store_number_one == this.form.store_number_two || this.form.store_number_one == this.form.store_number_third) {
					 this.$toast('请填写不同的商户号!');
					return false;
				}

				if (this.form.store_termno_one == this.form.store_termno_two || this.form.store_termno_one == this.form.store_termno_third) {
					 this.$toast('请填写不同的终端号!');
					return false;
				}
				
				this.form.brand_id = 12;
				this.$api.post({
				  url: 'portal/my/update_brand',
				  data: this.form,
				  success: res => {
					  if(res.code == 1){
						  this.form = res.data;
						  this.$toast('修改成功!');
						  uni.switchTab({
						  	url:'/pages/my/user/user'
						  })
					  }
				  }
				});
			}
		}
	}


	// //单图上传图片
	//     $('#piccc').change(function (event) {
	//         layer.load(1,{shade: [0.1,'#000']});
	//         var formData = new FormData();
	//         formData.append("file", $(this).get(0).files[0]);
	//         $.ajax({
	//             url: "{:url('wxapi/Public/uploadPic')",
	//             type: 'post',
	//             data: formData,
	//             cache: false,
	//             contentType: false,    //不可缺
	//             processData: false,    //不可缺
	//             success: function (data) {
	//                 console.log(data)
	//                 $(".pic").attr('src',data.src);
	//                 $("#avatar").val(data.src);
	//                 layer.closeAll();

	//             },
	//             error:function(res){
	//                 this.$toast(JSON.stringify(res))
	//             }
	//         });
	//     });

	//     function update_user() {

	//         var avatar = $('#avatar').val();
	//         var nickname = $('#nickname').val();
	//         var mobile = $('#mobile').val();
	//         var pwd = $("#password").val();
	// 		var store_name_one = $("#store_name_one").val();
	// 		var store_number_one = $("#store_number_one").val();
	// 		var store_termno_one = $("#store_termno_one").val();
	// 		var store_name_two = $("#store_name_two").val();
	// 		var store_number_two = $("#store_number_two").val();
	// 		var store_termno_two = $("#store_termno_two").val();
	// 		var contact = $("#contact").val();
	// 		if(!nickname){
	// 			this.$toast('请输入昵称!');
	// 		}
	// 		if(!mobile){
	// 			this.$toast('请输入手机号!');
	// 		}

	// 		if(mobile){
	// 	  		//正则表达式验证手机号是否正确
	// 	        var myreg = /^(13|14|15|16|17|18|19)[0-9]{9}$/;
	// 	            if(!myreg.test(mobile)){
	// 	              this.$toast('请输入正确的手机号!');
	// 	              return false;
	// 	        }
	// 		}

	//         if(store_name_one == store_name_two){
	//             //  this.$toast('请选择不同的终端品牌!');
	// 	         return false;
	//          }

	//         if(store_number_one == store_number_two){
	//             //  this.$toast('请填写不同的商户号!');
	// 	         return false;
	//          }

	//          if(store_termno_one == store_termno_two){
	//             //  this.$toast('请填写不同的终端号!');
	// 	         return false;
	//          }

	//         $.ajax({
	//             url: "{:url('my/update_user')",
	//             type: 'post',
	//             data: {
	//                 avatar:avatar,
	//                 nickname:nickname,
	//                 mobile:mobile,
	//                 pwd: pwd,
	// 				store_name_one:store_name_one,
	// 				store_number_one:store_number_one,
	// 				store_termno_one:store_termno_one,
	// 				store_name_two:store_name_two,
	// 				store_number_two:store_number_two,
	// 				store_termno_two:store_termno_two,
	// 				contact: contact,
	//             },
	//             success: function (data) {
	// 				console.log(data.msg);
	// 				var data = JSON.parse(data);
	// 				this.$toast(data.msg)
	//                  //window.location.href = "{:url('my/user')";
	//             },
	//             error:function(res){
	//                 alert(JSON.stringify(res))
	//             }
	//         });
	//     }

	// 	$('#select').change(function() {
	// 		var type = $("#select option:selected").val();
	// 		$('.choice_one').css('display','none');
	// 		$('.choice_two').css('display','none');
	// 		if(type == 1) {
	// 			$('.choice_one').css('display','block');
	// 		}else if(type == 2) {
	// 			$('.choice_two').css('display','block');
	// 		}
	// 	})
</script>

<style lang="scss" scoped>
	>html,
	body {
		height: 100%;
		background: #f1f1f1;
		font-size: 14px;
	}

	/deep/ .line {
		color: $u-light-color;
		font-size: 28rpx;
	}

	.wrap {
		background-color: #f2f2f2;

		.top {
			background-color: #ffffff;
			border-top: solid 2rpx $u-border-color;
			padding: 22rpx;

			.item {
				display: flex;
				font-size: 32rpx;
				line-height: 100rpx;
				align-items: center;
				border-bottom: solid 2rpx $u-border-color;

				.left {
					width: 180rpx;
				}

				input {
					text-align: left;
				}
			}

			.address {
				padding: 20rpx 0;

				textarea {
					// width: 100%;
					height: 150rpx;
					background-color: #f7f7f7;
					line-height: 60rpx;
					margin: 40rpx auto;
					padding: 20rpx;
				}
			}

			.site-clipboard {
				padding-right: 40rpx;

				textarea {
					// width: 100%;
					height: 150rpx;
					background-color: #f7f7f7;
					line-height: 60rpx;
					margin: 40rpx auto;
					padding: 20rpx;
				}

				.clipboard {
					display: flex;
					justify-content: center;
					align-items: center;
					font-size: 26rpx;
					color: $u-tips-color;
					height: 80rpx;

					.icon {
						margin-top: 6rpx;
						margin-left: 10rpx;
					}
				}
			}
		}

		.bottom {
			margin-top: 20rpx;
			padding: 40rpx;
			padding-right: 0;
			background-color: #ffffff;
			font-size: 28rpx;

			.default {
				display: flex;
				justify-content: space-between;
				border-bottom: solid 2rpx $u-border-color;
				line-height: 64rpx;

				.tips {
					font-size: 24rpx;
				}

				.right {}
			}
		}
	}


	.displayFlex {
		display: flex;
	}

	.flex {
		flex: 1;
		text-align: center;
	}

	.flex_xy {
		display: flex;
		align-items: center;
		/*垂直居中*/
		justify-content: center;
		/*水平居中*/
	}

	.avatar {
		height: 10rem;
		background: url("https://shop.zdsy.shop/themes/screens/public/assets/images/my_information.jpg")no-repeat;
		-webkit-background-size: 100% 100%;
		background-size: 100% 100%;
	}

	.id_card_content {
		position: relative;
		height: 4rem;
		width: 4rem;
	}

	img {
		width: 100%;
		height: 100%;
	}

	.pic {
		border-radius: 50%;
	}

	a:link {
		text-decoration: none;
	}

	a:visited {
		text-decoration: none;
	}

	a:hover {
		text-decoration: none;
	}

	a:active {
		text-decoration: none;
	}

	.button.button-fill {
		background: #DF2E26;
		color:#fff
	}

	.choice {
		display: none;
	}
</style>
